package com.jf.cloud.unicomdata.controller.admin;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.unicomdata.dto.ScenicSpotDTO;
import com.jf.cloud.unicomdata.model.ScenicSpot;
import com.jf.cloud.unicomdata.service.ScenicSpotService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author zz
 * @date 2023-04-24 18:39:33
 */
@RestController("appScenicSpotController")
@RequestMapping("/scenic_spot")
@Tag(name = "")
public class ScenicSpotController {

    @Autowired
    private ScenicSpotService scenicSpotService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取列表", description = "分页获取列表")
    public ServerResponseEntity<PageVO<ScenicSpot>> page(@Valid PageDTO pageDTO) {
        PageVO<ScenicSpot> scenicSpotPage = scenicSpotService.page(pageDTO);
        return ServerResponseEntity.success(scenicSpotPage);
    }

    @GetMapping
    @Operation(summary = "获取", description = "根据id获取")
    public ServerResponseEntity<ScenicSpot> getById(@RequestParam String code) {
        return ServerResponseEntity.success(scenicSpotService.getNameByCode(code));
    }

    @PostMapping
    @Operation(summary = "保存", description = "保存")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ScenicSpotDTO scenicSpotDTO) {
        ScenicSpot scenicSpot = mapperFacade.map(scenicSpotDTO, ScenicSpot.class);
        scenicSpotService.save(scenicSpot);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新", description = "更新")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ScenicSpotDTO scenicSpotDTO) {
        ScenicSpot scenicSpot = mapperFacade.map(scenicSpotDTO, ScenicSpot.class);
        scenicSpotService.update(scenicSpot);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除", description = "根据id删除")
    public ServerResponseEntity<Void> delete(@RequestParam Long id) {
        scenicSpotService.deleteById(id);
        return ServerResponseEntity.success();
    }
}
